<template>
	<view>
		        <uni-card style="height: 320px ;margin: 15px" >
		          <uni-forms ref="formRef" :modelValue="users" :rules="rules" label-width="80px" style="padding-right: 20px">
		            <uni-forms-item label="原密码" required name="password">
		              <uni-easyinput show-password v-model="users.password" placeholder="原密码"></uni-easyinput>
		            </uni-forms-item>
		            <uni-forms-item label="新密码" required name="newPassword">
		              <uni-easyinput show-password v-model="users.newPassword" placeholder="新密码"></uni-easyinput>
		            </uni-forms-item>
		            <uni-forms-item label="确认密码" required name="confirmPassword">
		              <uni-easyinput show-password v-model="users.confirmPassword" placeholder="确认密码"></uni-easyinput>
		            </uni-forms-item>
		            <view style="text-align: center; margin-bottom: 20px">
		              <button type="primary" @click="update">确认修改</button>
		            </view>
		          </uni-forms>
		        </uni-card>
	</view>
</template>

<script>
	export default{
	data() {
		//修改密码
		    const validatePassword = (rule, value, callback) => {
		      if (value === '') {
		        callback(new Error('请确认密码'))
		      } else if (value !== this.users.newPassword) {
		        callback(new Error('确认密码错误'))
		      } else {
		        callback()
		      }
		    }
		//
		return {
			  users: uni.getStorageSync('weather2024'),
			  //修改密码
			       show:true,
			        rules: {
			          password: {
						  rules:[{
							  required: true,
							   errorMessage: '请输入原始密码',
							    validateTrigger:'submit'
						  }]
			          },
					 newPassword: {
					  						  rules:[{
					  							  required: true,
					  							   errorMessage: '请输入新密码',
					  							    validateTrigger:'submit',
													
					  						  }]
					  },
					  confirmPassword: {
					  						  rules:[{
					  							  required: true,
					  							  validator:validatePassword,
					  							    validateTrigger:'submit'
					  						  }]
					  },
			         
			        }
		}
		},
		methods:{
			
			//更新密码到数据库
			    update() {
			     this.$refs.formRef.validate().then(res => {
			         this.users.password = this.users.newPassword
			          // 保存当前的用户信息到数据库
			          this.$request.put('/user/update', this.users).then(res => {
			            if (res.code === '200') {
			              // 成功更新
			             uni.showToast({
							 icon:'success',
			                title:'修改成功'			   
			             })
			             uni.redirectTo({
			             	url: '/pages/login/login'
			             })
						 uni.setStorageSync('weather2024', res.data)
			            }
			          })
			        })
					}
			       
			    },
		}

	
</script>

<style>
</style>